<?php

include_once($mcrp . 'config/config.php');

function CacheCheckValidity($md5, $time)
{
	global $mcrp;
	$url = $mcrp . 'cache/' . $md5;
	if(file_exists($url) == false) return false;
	if($time <= 0) return true;
	$filetime = filemtime($url);
	if($filetime < (time() - (int)$time)) return false;
	return true;
}

function UpdateCacheContent($file, $md5)
{
	global $mcrp;
	ob_start();
	extract($GLOBALS, EXTR_REFS);
	include($mcrp . $file);
	$page = ob_get_contents();
	ob_end_clean();
	file_put_contents($mcrp . 'cache/' . $md5, $page);
}

function ResetCacheEntry($file, $param = '')
{
	return ResetCacheEntryByMD5(md5($file . $param));
}

function ResetCacheEntryByMD5($md5)
{
	global $mcrp;
	$url = $mcrp . 'cache/' . $md5;
	if(file_exists($url) == true)
		unlink($url);
	return true;
}

function ResetCacheParent($parent)
{

}

function DisplayCacheContent($file, $time = 0, $parent = array(), $param = '')
{
	global $mcrp;
	$md5 = md5($file . $param);
	if(CacheCheckValidity($md5, $time) == false)
	{
		UpdateCacheContent($file, $md5);
	}
	extract($GLOBALS, EXTR_REFS);
	include($mcrp . 'cache/' . $md5);
}

function GetCacheContent($file, $time = 0, $parent = array(), $param = '')
{
	ob_start();
	DisplayCacheContent($file, $time, $parent, $param);
	$res = ob_get_contents();
	ob_end_clean();
	return $res;
}

?>